package cn.gudian.taglib.cms.news

import cn.gudian.cms.News.ArcType

class ArcTypeTagLib {
  static namespace = "gd"    //定义该系统的命名空间
  //输出所有属性

  void outputAttributes(attrs) {
    attrs.remove('tagName') // Just in case one is left
    attrs.each { k, v ->
      out << k << "=\"" << v.encodeAsHTML() << "\" "
    }
  }

  /**
   * 显示某个所有的栏目列表和相关操作
   * 前台使用方式：<mtsystem: estateID="1"  nbsp="1"></mtsystem:arcTypeList>
   *
   * 必须传入参数 ：estateID和 nbsp
   * @param attrs
   *            - 参数集合
   * @param body
   *             - 输出内容
   *
   */
  def arcTypeList = {attrs, body ->

    def arcTypeID = attrs.get("arcTypeID"); //取得栏目ID
    def nbsp = Integer.parseInt(attrs.get("nbsp")); //取得空格数量
    out << " <table><thead> <tr>  "
    out << "<th width='40'>选择</th>  "
    out << "<th>名称</th>  "
    out << "<th>排序</th>"
    out << "<th>模块</th>"
    out << "<th>修改</th> "
    out << "<th>生成HTML</th> "
    out << "</tr></thead><tbody> "


    showarcTypeList(arcTypeID, nbsp)

    out << " </tbody></table>"

  }

  /**
   * 所有栏目列表，重新按照要求显示的list页面
   *
   * @param estateID
   *           String - 楼盘ID
   * @param arcTypeID
   *            String - 栏目ID，如果为空的显示该楼盘下面的所有栏目
   * @param nbsp
   *            String - 空格数量，用来使根栏目与子栏目显示有层级关系
   *
   */
  def showarcTypeList = {arcTypeID, nbsp ->

    def shownbsp = ""
    for (i in 0..<nbsp) {
      shownbsp = shownbsp + "&nbsp;"
    }

    def arcTypeInstanceTopList
    if (arcTypeID == null) {
      arcTypeInstanceTopList = ArcType.findAll("from ArcType where parent=null order by orderNum asc")
      arcTypeInstanceTopList?.each {
        out << "<tr>"
        out << "<td><input name='listId' type='checkbox'' id='listId' value='${it.id}'/></td>  "
        out << "    <td>" + shownbsp + "<img src=" + createLinkTo(dir: 'images', file: 'm-expand.gif') + " align='absmiddle' />&nbsp;&nbsp;<a href=" + createLink(action: 'show', id: it.id) + ">" + it.name + "</a>[<a href=" + createLink(action: 'create', params: [id: it.id, mode: it.mode]) + ">增加子栏目</a>]</td> "

        out << "     <td>" + it.orderNum + "</td> "
        out << "    <td>" + it.modeStates.get(it.mode) + "</td>"
        out << "    <td><a href=" + createLink(action: 'edit', id: it.id) + "><img src=" + createLinkTo(dir: 'images/skin', file: 'database_edit.png') + " align='absmiddle' /></a></td> "
        out << "    <td><a href=" + createLink(controller: 'make', action: 'makeArcType', id: it.id) + ">生成HTML</a> </td>"

        out << " </tr> "
        showarcTypeList(it.id, nbsp + 6)
      }
    } else {
      def arcTypeInstance = ArcType.get(arcTypeID);
      arcTypeInstanceTopList = ArcType.findAll("from ArcType  where  parent=? order by orderNum asc", [arcTypeInstance])//取得某个楼盘的顶级栏目
      arcTypeInstanceTopList?.each {
        out << "<tr >"
        out << "<td><input name='listId' type='checkbox'' id='listId' value='${it.id}'/></td>  "
        out << "    <td>" + shownbsp + ".&nbsp;&nbsp;<a href=" + createLink(action: 'show', id: it.id) + ">" + it.name + "</a>[<a href=" + createLink(action: 'create', params: [id: it.id, mode: it.mode]) + ">增加子栏目</a>]</td> "
        out << "     <td>" + it.orderNum + "</td> "
        out << "    <td>" + it.modeStates.get(it.mode) + "</td>"
        out << "    <td><a href=" + createLink(action: 'edit', id: it.id) + "><img src=" + createLinkTo(dir: 'images/skin', file: 'database_edit.png') + " align='absmiddle' /></a></td> "
        out << "    <td><a href=" + createLink(controller: 'make', action: 'makeArcType', id: it.id) + ">生成HTML</a> </td>"

        out << " </tr> "
        showarcTypeList(it.id, nbsp + 6)
      }
    }
  }

  /**
   * 用select标签输出所有栏目或者某个栏目的子栏目
   * 前台使用方式：<gd:arcTypeSelect estateID="1" arcTypeID="7" nbsp="1" id="parent.id"></gd:arcTypeSelect>
   * 或者：<gd:arcTypeSelect estateID="1"  nbsp="1" id="parent.id"></gd:arcTypeSelect>
   * 必须传入参数 ：estateID和 nbsp
   * @param attrs
   *            - 参数集合
   * @param body
   *             - 输出内容
   *
   */
  def arcTypeSelect = {attrs, body ->
    def nowID = attrs?.nowID
    def arcTypeID = attrs?.arcTypeID
    def mode = attrs?.mode
    def nbsp = Integer.parseInt(attrs.get("nbsp")); //取得空格数量
    if (mode == null)
      throwTagError("标签[arcTypeSelect]的属性[mode]不能为空")

    def selectID = attrs.get("id");  //取得selectb标签的name和id
    out << "<select id=\"${selectID}\" name=\"${selectID}\""
    outputAttributes(attrs)
    out << '>'
    out << body()
    showarcTypeSelect(arcTypeID, nbsp, mode, nowID)
    out << "</select>"

  }

  /**
   * 用select标签输出某个mode的所有栏目或者某个栏目的子栏目
   *
   * @param estateID
   *           String - 楼盘ID
   * @param arcTypeID
   *            String - 栏目ID，如果为空的显示该楼盘下面的所有栏目
   * @param nbsp
   *            String - 空格数量，用来使根栏目与子栏目显示有层级关系
   *
   */
  def showarcTypeSelect = {arcTypeID, nbsp, mode, nowID ->

    def shownbsp = ""
    for (i in 0..<nbsp) {
      shownbsp = shownbsp + "&nbsp;"
    }


    def arcTypeInstanceTopList
    if (arcTypeID == null) {
      arcTypeInstanceTopList = ArcType.findAll("from ArcType where parent=null and mode=?", [mode])   //取得某个楼盘的顶级栏目

      arcTypeInstanceTopList?.each {
        String selected = (nowID == it.id) ? "selected='selected'" : ""
        out << "<option value=" + it.id + " " + selected + ">" + shownbsp + it.name + "</option>"

        showarcTypeSelect(it.id, nbsp + 6, mode, nowID)
      }

    } else {
      def arcTypeInstance = ArcType.get(arcTypeID);
      arcTypeInstanceTopList = ArcType.findAll("from ArcType  where parent=? and mode=?", [arcTypeInstance, mode]) //取得某个楼盘的顶级栏目
      arcTypeInstanceTopList?.each {
        String selected = (nowID == it.id) ? "selected='selected'" : ""
        out << "<option value=" + it.id + "  " + selected + ">" + shownbsp + "┞" + it.name + "</option>"
        showarcTypeSelect(it.id, nbsp + 6, mode, nowID)
      }

    }

  }
  //栏目多选
  def arcTypeMultipleSelect = {attrs, body ->

    def arcTypeID = attrs.get("arcTypeID"); //取得栏目ID
    def nbsp = Integer.parseInt(attrs.get("nbsp")); //取得空格数量
    def size = attrs.get("size") ? attrs.get("size") : "10";
    def selectID = attrs.get("id");  //取得selectb标签的name和id
    out << "<select Multiple='true' size='" + size + "' id='" + selectID + "' name='" + selectID + "' >"
    out << "<option value='0'>全部可管理</option>"
    showarcTypeAllSelect(arcTypeID, nbsp)
    out << "</select>"

  }

  /**
   * 用select标签输出某个mode的所有栏目或者某个栏目的子栏目
   *
   * @param estateID
   *           String - 楼盘ID
   * @param arcTypeID
   *            String - 栏目ID，如果为空的显示该楼盘下面的所有栏目
   * @param nbsp
   *            String - 空格数量，用来使根栏目与子栏目显示有层级关系
   *
   */
  def showarcTypeAllSelect = {arcTypeID, nbsp ->

    def shownbsp = ""
    for (i in 0..<nbsp) {
      shownbsp = shownbsp + "&nbsp;"
    }


    def arcTypeInstanceTopList
    if (arcTypeID == null) {
      arcTypeInstanceTopList = ArcType.findAll("from ArcType where parent=null order by orderNum asc")   //取得某个楼盘的顶级栏目

      arcTypeInstanceTopList?.each {

        out << "<option value=" + it.id + ">" + shownbsp + it.name + "</option>"

        showarcTypeAllSelect(it.id, nbsp + 6)
      }

    } else {
      def arcTypeInstance = ArcType.get(arcTypeID);
      arcTypeInstanceTopList = ArcType.findAll("from ArcType  where parent=? order by orderNum asc", [arcTypeInstance]) //取得某个楼盘的顶级栏目
      arcTypeInstanceTopList?.each {

        out << "<option value=" + it.id + ">" + shownbsp + "┞" + it.name + "</option>"
        showarcTypeAllSelect(it.id, nbsp + 6)
      }

    }

  }
}
